<?php

include_once ('../sys/sys.php');
include_once ('../sys/json/JSON.php');

header('Content-Type: text/html; charset=utf-8');

$action = $_GET['action'];
$tid = $_GET['tid'];
$st = $_GET['st'];
$ord = $_GET['ord'];
$ort = $_GET['ort'];
$cid = $_GET['cid'];
$jid = $_GET['jid'];
if (is_null($st)) {
	$st = 0;
}
$listnum = 19;

if (! is_numeric($tid) || $tid < 0) {
	show_normal_mesR('TAG ID错误', 'TAG ID错误', 'TAG查询');
}

if ($action == 'getpicurls') {
	// 获取该分类下所有应用ID
	$DB->build_query(array(
			'select' => 'id', 
			'from' => 'gfunapps', 
			'where' => 'tid=' . $tid));
	$qid = $DB->exec_query();
	$pics = '';
	while ( $row = $DB->fetch_row($qid) ) {
		// 基于应用ID查出对应图片链接
		$DB->build_query(array(
				'select' => 'imgorgurl', 
				'from' => 'gfunpics', 
				'where' => 'appid=' . $row['id']));
		$pid = $DB->exec_query();
		while ( $row = $DB->fetch_row($pid) ) {
			$pics .= $row['imgorgurl'] . "\r\n";
		}
	}
	echo '<textarea rows="20" cols="80" tabindex="2" class="txtarea">' . $pics . '</textarea>';
	
	exit();

} else if ($action == 'getpicfiles') {
	// 获取该分类下所有应用ID
	$DB->build_query(array(
			'select' => 'id', 
			'from' => 'gfunapps', 
			'where' => 'tid=' . $tid));
	$qid = $DB->exec_query();
	$pics = '';
	while ( $row = $DB->fetch_row($qid) ) {
		// 基于应用ID查出对应图片链接
		$DB->build_query(array(
				'select' => 'downorgurl', 
				'from' => 'gfunappds', 
				'where' => 'appid=' . $row['id']));
		$pid = $DB->exec_query();
		while ( $row = $DB->fetch_row($pid) ) {
			$pics .= $row['downorgurl'] . "\r\n";
		}
	}
	echo '<textarea rows="20" cols="80" tabindex="2" class="txtarea">' . $pics . '</textarea>';
	
	exit();
} else if ($action == 'getlogopicurls') {
	// 获取该分类下所有应用图标
	$DB->build_query(array(
			'select' => 'orglogo', 
			'from' => 'gfunapps', 
			'where' => 'tid=' . $tid));
	$qid = $DB->exec_query();
	$pics = '';
	while ( $row = $DB->fetch_row($qid) ) {
		$pics .= $row['orglogo'] . "\r\n";
	}
	echo '<textarea rows="20" cols="80" tabindex="2" class="txtarea">' . $pics . '</textarea>';
	
	exit();
} else if ($action == 'showapplinks') {
	// 显示所有应用列表
	$DB->build_query(
			array(
					'select' => 'id,name', 
					'from' => 'gfunapps', 
					'where' => 'tid=' . $tid, 
					'limit' => array(
							$st, 
							$listnum)));
	$qid = $DB->exec_query();
	$i = 0;
	while ( $row = $DB->fetch_row($qid) ) {
		$array_piclist[$i]['name'] = $row['name'];
		$array_piclist[$i]['id'] = $row['id'];
		$array_piclist[$i]['appdetail'] = SITE_URL . "gfun/pics.php?action=showappdetail&tid=" . $row['id'];
		$i ++;
	}
	$totalnumbers = get_count_of_field('gfunapps', ' tid=' . $tid);
	$pagelink = $Elsesky->PageLinkR($st, $totalnumbers, $listnum, SITE_URL . "gfun/pics.php?action=showapplinks&tid=" . $tid . "&st=<:st:>");
	$page = init_smart_template_obj('gfun/app.html', CACHE_DIR, CACHE_DIR, TEMPLATE_DIR);
	$page->assign("piclist", $array_piclist);
	$page->assign("pagelink", $pagelink);
	$page->assign("system", $sysinfo);
	$page->assign("title", "列表");
	$page->assign("tidlink", $array_ccount);
	$page->output();
	
	exit();
} else if ($action == 'showapplinksjson') {
	// 显示所有应用列表
	$orders = array(
			"id", 
			"downloadcount");
	$ordact = array(
			"ASC", 
			"DESC");
	$ord = is_numeric($ord) ? intval($ord) : 0;
	$ord = $ord <= 0 ? 0 : $ord;
	$ord = $ord >= count($orders) ? 0 : $ord;
	$ort = is_numeric($ort) ? intval($ort) : 0;
	$ort = $ort <= 0 ? 0 : $ort;
	$ort = $ort >= 1 ? 1 : $ort;
	$order = $orders[$ord] . " " . $ordact[$ort];
	$cache = "d:\\web\\cache\\showapplinksjson\\" . $tid . "-" . $st . "-" . $ord . "-" . $ort . ".json";
	if (file_exists($cache)) {
		$ret = file_get_contents($cache);
		if ($ret === FALSE || mb_strlen($ret, "utf-8") <= 0) {
			// do nothing
			unlink($cache);
		} else {
			// 读取内容之后直接打印
			print $ret;
			exit();
		}
	}
	$DB->build_query(
			array(
					'select' => 'id,name,ver,size,author,lastupdate,downloadcount', 
					'from' => 'gfunapps', 
					'where' => 'tid=' . $tid . ' and isup=0', 
					'order' => $order, 
					// 'limit' => array($st,$listnum),
					'limit' => array(
							$st, 
							10)));
	$qid = $DB->exec_query();
	$i = 0;
	$array_piclistjson = array();
	
	while ( $row = $DB->fetch_row($qid) ) {
		$DB->build_query(array(
				'select' => 'imgurl', 
				'from' => 'gfunpics', 
				'where' => 'appid=' . $row['id']));
		$path = "D:\\web\\hf\\downloadpic";
		$count = 0;
		$images = array();
		$pid = $DB->exec_query();
		while ( $prow = $DB->fetch_row($pid) ) {
			// 循环判断文件路径
			if ($prow["imgurl"] == "default_icon.png") {
				// do nothing
			} else {
				$filename = $path . "\\" . $prow["imgurl"];
				if (file_exists($filename)) {
					array_push($images, $prow["imgurl"]);
					$count ++;
				}
			}
		}
		
		array_push($array_piclistjson, 
				array(
						"name" => $row['name'], 
						"id" => $row['id'], 
						"ver" => $row['ver'], 
						"size" => $row['size'], 
						"author" => $row['author'], 
						"lastupdate" => date("Y/m/j", $row['lastupdate']), 
						"downloadcount" => $row['downloadcount'], 
						"images" => $images, 
						"imagecount" => $count));
		// $array_piclistjson[$i]['name'] = $row['name'];
		// $array_piclistjson[$i]['id'] = $row['id'];
		// $i++;
	}
	$a = array(
			"data" => $array_piclistjson);
	$out = json_encode($a);
	file_put_contents($cache, $out);
	print $out;
	// var_dump($a);
	// $array_piclistjson['totalnum'] = $totalnumbers;
	// $totalnumbers = get_count_of_field('gfunapps',' tid=' . $tid);
	// $json = new Services_JSON();
	// $output = $json->encode($array_piclistjson);
	// echo $output;
	
	exit();
} else if ($action == 'showappdetail') {
	
	// echo $tid;exit;
	
	$row = $DB->build_and_exec_query(array(
			'select' => '*', 
			'from' => 'gfunapps', 
			'where' => 'id=' . $tid));
	// 获取所有图片
	$DB->build_query(array(
			'select' => 'imgurl', 
			'from' => 'gfunpics', 
			'where' => 'appid=' . $tid));
	$picorgurls = array();
	$qid = $DB->exec_query();
	while ( $rowt = $DB->fetch_row($qid) ) {
		$picorgurls[] = $rowt['imgurl'];
	}
	// 获取下载列表
	$rowdl = $DB->build_and_exec_query(array(
			'select' => 'downurl', 
			'from' => 'gfunappds', 
			'where' => 'appid=' . $tid));
	// 获取
	// $rowimg = $DB->build_and_exec_query(array(
	// 'select' => 'imgurl',
	// 'from' => 'gfunpics',
	// 'where' => 'appid=' . $tid,
	// ));
	// 逆查ID
	$rowtname = $DB->build_and_exec_query(array(
			'select' => 'name', 
			'from' => 'gfuntags', 
			'where' => 'id=' . $row['tid']));
	// 判断是否默认图标,如果是，留空
	if (strpos($picorgurls[0], "_icon.png")) {
		$picorgurls[0] = '';
	}
	$row['picorgurls'] = $picorgurls;
	
	// print_r($row);exit;
	// foreach ($rowimg as $k => $v) {
	// $row[$k] = $v;
	// }
	foreach ( $rowdl as $k => $v ) {
		$row[$k] = $v;
	}
	unset($row['orglogo']);
	$row['tagname'] = $rowtname['name'];
	
	// print_r($row);exit;
	
	$json = new Services_JSON();
	$output = $json->encode($row);
	echo $output;
	// print_r($row);
	
	exit();
}

?>